package com.d6game.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import weibo4j.Friendships;
import weibo4j.Timeline;
import weibo4j.examples.oauth2.Log;
import weibo4j.http.AccessToken;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;

import com.d6game.web.annotation.LoginRequired;
import com.d6game.web.service.UserService;

@Controller
@RequestMapping(value = "/weibo")
public class WeiboController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(WeiboController.class);  
	@Autowired
	UserService userService;
	
	@LoginRequired
	@RequestMapping(value="/home")
	public String home(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model){
		AccessToken accessToken = (AccessToken) session.getAttribute("accessToken");
		String access_token = accessToken.getAccessToken();
		Timeline tm = new Timeline();
		tm.client.setToken(access_token);
		Friendships fm = new Friendships();
		fm.client.setToken(access_token);
		try {
			StatusWapper status = tm.getUserTimeline();
			model.addAttribute("status", status);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return "weibo/wb_weibo";
	}
	@LoginRequired
	@RequestMapping(value="/home/{statusId}")
	public String showStatus(@PathVariable("statusId") String statusId,HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model){
		String p = request.getParameter("page");
		if(null == p || "".equals(p)){
			p="1";
		}
		AccessToken accessToken = (AccessToken) session.getAttribute("accessToken");
		String access_token = accessToken.getAccessToken();
		User user  = (User) session.getAttribute("wb_user");
		Timeline tm = new Timeline();
		tm.client.setToken(access_token);
		Friendships fm = new Friendships();
		fm.client.setToken(access_token);
		Paging page = new Paging(Integer.valueOf(p), 50);
		try {
			Status status = tm.showStatus(statusId);
			model.addAttribute("status", status);
			UserWapper users = fm.getFriendsInCommon(user.getId(), user.getId(), page);
			model.addAttribute("users", users);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return "weibo/wb_weibo_detail";
	}
	@LoginRequired
	@RequestMapping(value="/myfollow")
	public String myfollow(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model){
		User user  = (User) session.getAttribute("wb_user");
		if(null == user){
			return "redirect:/";
		}
		String uid = user.getId();
		AccessToken accessToken = (AccessToken) session.getAttribute("accessToken");
		String access_token = accessToken.getAccessToken();
		Friendships fm = new Friendships();
		fm.client.setToken(access_token);
		try {
			UserWapper users = fm.getFriendsByID(uid);
			model.addAttribute("users", users);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return "weibo/wb_myfollow";
	}
}
